package com.hgy.sort;

import java.text.SimpleDateFormat;
import java.util.Date;

public class QuickSort {
    public static void main(String[] args) {
        int[] arr = {32, 12,312,42,46,2,65,43,67,1};
        quickSort(arr, 0, arr.length-1);
        print(arr);

//        int num = 800000;
//        int arr[] = new int[num];
//        for (int i = 0; i < num; i++) {
//            arr[i] = (int)(Math.random() * num);
//        }
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date d1 = new Date();
//        String d1str = simpleDateFormat.format(d1);
//        System.out.println(d1str);
//
//        quickSort(arr, 0, arr.length-1);
//
//        Date d2 = new Date();
//        String d2str = simpleDateFormat.format(d2);
//        System.out.println(d2str);
    }

    public static void print(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    public static void quickSort(int[] arr, int left, int right){
        if (left < right){
            int pivot = partition(arr, left, right);
            quickSort(arr, left, pivot - 1);
            quickSort(arr, pivot + 1, right);
        }
    }

    public static int partition(int[] arr, int left, int right){
        int index = arr[left];
        while (left < right){
            while (left<right && arr[right] >= index){
                right--;
            }
            if (left < right) {
                arr[left] = arr[right];
            }
            while (left<right && arr[left] <= index){
                left++;
            }
            if (left<right) {
                arr[right] = arr[left];
            }
        }
        arr[left] = index;
        return left;
    }
}
